package com.alibaba.csp.sentinel.dashboard.rule.nacos;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.dashboard.util.ConverterUtils;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 网关api流控 从nacos中读取规则
 * @author cjunxian
 * @date 2021/6/25
 */
@Component("GatewayApiRuleNacosProvider")
public class GatewayApiRuleNacosProvider implements DynamicRuleProvider<List<ApiDefinitionEntity>> {

    @Autowired
    private ConfigService configService;

    @Autowired
    ConverterUtils converterUtils;

    @Override
    public List<ApiDefinitionEntity> getRules(String appName) throws Exception {
        return converterUtils.pullRule(configService
                ,appName
                ,NacosConfigUtil.GATEWAY_API_DATA_ID_POSTFIX
                ,NacosConfigUtil.GROUP_ID
                ,3000
                ,ApiDefinitionEntity.class);
    }
}
